package com.hunk.core.tiles.preparer;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.hunk.utils.io.Print;

@Component
public class TestPreparer implements ViewPreparer {

	@Autowired
	JdbcTemplate jdbcTemplate;
	@Override
	public void execute(TilesRequestContext trc, AttributeContext ac) {
		String day = (String) trc.getParam().get("day");
		Print.println("In TestViewPreparer : day = " + day);
		
		Object[] requestObjects = trc.getRequestObjects();
		if (requestObjects[0] instanceof HttpServletRequest) {
			HttpServletRequest request = (HttpServletRequest) requestObjects[0];
			if (!StringUtils.isNotEmpty(day)) day = "2013-03-23"; 
			List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from view_component where update_date = '" + day + "' order by orders");
			request.setAttribute("view_cop", list);
		}		
	}
	
}
